package xyz.gpgl.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.fasterxml.jackson.databind.SerializationFeature;
import common.util.mybatis.plus.handler.LogicMetaObjectHandler;
import common.util.mybatis.plus.handler.LogicMetaObjectHandlerService;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import xyz.gpgl.config.custom.MyDispatcherServlet;
import xyz.gpgl.erp.sys.service.impl.MyUserDetailServiceImpl;

import javax.sql.DataSource;

@Configuration
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableConfigurationProperties
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 事务配置
     *
     * @author stylefeng
     * @Date 2018/6/27 23:11
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * mybatis-puls 自动填充字段
     * @return
     */
    @Bean
    public MetaObjectHandler metaObjectHandler(){
        return new LogicMetaObjectHandler(new LogicMetaObjectHandlerService(){
            @Override
            public String getOperator() {
                MyUserDetailServiceImpl.MyUserDetails user = null;
                try {
                    user = (MyUserDetailServiceImpl.MyUserDetails) SecurityContextHolder
                            .getContext().getAuthentication().getPrincipal();
                    return user.getUsername();
                }catch (Exception e){
                    return "system";
                }
            }
        });
    }

    /**
     * mybatis-plus 乐观锁
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 自定义 MyDispatcherServlet
     * @return
     */
    @Bean
    public xyz.gpgl.config.custom.MyDispatcherServlet myDispatcherServlet(){
        return new MyDispatcherServlet();
    }

    /**
     * 注册自定义的 dispatcher
     * @param myDispatcherServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean<MyDispatcherServlet> dispatcherRegistration(MyDispatcherServlet myDispatcherServlet) {
        ServletRegistrationBean<MyDispatcherServlet> registration = new ServletRegistrationBean<>(myDispatcherServlet);
        return registration;
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(){
        return builder -> builder.featuresToEnable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
    }
    @Bean(name="multipartResolver")
    public MultipartResolver multipartResolver(){
        return new CommonsMultipartResolver();
    }
}

